package com.jcp.util;

import static java.util.regex.Pattern.matches;

final public class VerifyUtil {

	private static String regPhoneNumber = "^1(3|4|5|6|7|8|9)\\d{9}$";
	private static String regUserName = "^\\w{3,20}$";
	private static String regIDCard = "^[1-9]\\d{5}(19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
	private static String[] regPwd = { "^[A-Za-z0-9_!@#$%&*?.+-]{6,20}$", "^[A-Za-z]{6,20}$", "^\\d{6,20}$",
			"^[_!@#$%&*?.+-]{6,20}$", "^[A-Za-z0-9]{6,20}$", "^[0-9_!@#$%&*?.+-]{6,20}$", "^[A-Za-z_!@#$%&*?.+-]{6,20}$" };
	private static 	String regUserId="[1-9]\\d*";
	
	public static boolean checkUserId(String userId) {
		return matches(regUserId, userId);
	}
		
	/**
	 * 验证身份证号码基本格式
	 * 
	 * @param idCardNumber
	 * @return 验证合法返回true 否则返回false
	 */
	public static boolean checkIDCard(String idCardNumber) {
		return matches(regIDCard, idCardNumber);
	}
	
	/**
	 * 验证手机号码
	 * 
	 * @param phoneNumber
	 * @return 验证合法返回true 否则返回false
	 */
	public static boolean checkPhoneNumber(String phoneNumber) {
		return matches(regPhoneNumber, phoneNumber);
	}
	
	/**
	 * 验证用户名 支持3-20个字母、数字及下划线
	 * 
	 * @param userName
	 * @return 验证合法返回true 否则返回false
	 */
	public static boolean checkUserName(String userName) {
		if (matches(regUserName, userName))
			return true;
		return false;
	}

	/**
	 * 验证密码格式 支持6-20个字母、数字、下划线及 !@#$%&?.+-*
	 * 
	 * @param pwd
	 * @return 不合法返回-1 强度低返回0 强度中返回1 强度高返回2
	 */
	public static int checkPwd(String pwd) {
		if (!matches(regPwd[0], pwd))
			return -1;
		if ((matches(regPwd[1], pwd) && !matches(regPwd[2], pwd) && !matches(regPwd[3], pwd))
				|| (!matches(regPwd[1], pwd) && matches(regPwd[2], pwd) && !matches(regPwd[3], pwd))
				|| (!matches(regPwd[1], pwd) && !matches(regPwd[2], pwd) && matches(regPwd[3], pwd)))
			return 0;
		if ((matches(regPwd[4], pwd) && !matches(regPwd[5], pwd) && !matches(regPwd[6], pwd))
				|| (!matches(regPwd[4], pwd) && matches(regPwd[5], pwd) && !matches(regPwd[6], pwd))
				|| (!matches(regPwd[4], pwd) && !matches(regPwd[5], pwd) && matches(regPwd[6], pwd)))
			return 1;
		return 2;
	}

	private VerifyUtil() {
	}

}
